package edu.spbu.eclipse.addressbook.larchickkudryavtsev.phonebookview.views;

import java.util.ArrayList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class ResultsDialog {
	private Table resultsTable;

	private Shell shell;

	private Display display;

	private void addColumn(String name, int width) {
		TableColumn column = new TableColumn(resultsTable, SWT.LEFT);

		column.setText(name);
		column.setWidth(width);
	}
	
	public void addItem(String[] data) {
		TableItem item = new TableItem(resultsTable, SWT.NONE);
		
		item.setText(data);
	}

	public ResultsDialog (Shell parentShell, ArrayList<String[]> data) {
		shell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		
		int width = shell.getParent().getSize().x;
		int height = shell.getParent().getSize().y;
		
		display = shell.getDisplay();
		shell.setBounds(width/2 - 238, height/2 - 113, 475, 215);
		shell.setText("Search Results:");		

		resultsTable = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		resultsTable.setLinesVisible(true);
		resultsTable.setHeaderVisible(true);
		resultsTable.setBounds(10, 10, 450, 160);

		addColumn("Name", 92);
		addColumn("Surname", 108);
		addColumn("Phone Number", 120);
		addColumn("E-mail", 125);
        
		for (int i = 0; i < data.size(); i++) {
			addItem(data.get(i));
		}
		
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}